package com.iss.umservice.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iss.umservice.bean.CompanyBean;
import com.iss.umservice.bean.MonthReportBean;
import com.iss.umservice.dao.CompanyDao;
import com.iss.umservice.dao.MonthReportDao;
import com.iss.umservice.manager.MonthReportManager;

@Service
public class MonthReportManagerImpl implements MonthReportManager
{
    @Autowired
    private MonthReportDao monthReportDao;
    
    @Autowired
    private CompanyDao companyDao;
    
    @Override
    public List<MonthReportBean> queryLastReport()
    {
        List<MonthReportBean> list = monthReportDao.queryLastReport();
        for (int i = 0; i < list.size(); i++)
        {
            MonthReportBean report = list.get(i);
            String compId = report.getCompId();
            if (compId != null && !"总计".equals(compId) && !"Total".equalsIgnoreCase(compId))
            {
                CompanyBean company = new CompanyBean();
                company.setId(Integer.parseInt(compId));
                CompanyBean companyBean = companyDao.getCompany(company);
                if(companyBean != null)
                {
                    report.setCompName(companyBean.getCompanyname());
                }
            }
        }
        
        return list;
    }
    
    @Override
    public void compBack(String compId)
    {
        MonthReportBean lastBean = monthReportDao.queryCompEndReport(compId);
        if (lastBean != null)
        { 
            if ("12".equals(lastBean.getMonthnum()))
            {
                lastBean.setYearnum(String.valueOf(Integer.parseInt(lastBean.getYearnum()) + 1));
                lastBean.setMonthnum("1");
            }
            else
            {
                lastBean.setMonthnum(String.valueOf(Integer.parseInt(lastBean.getMonthnum()) + 1));
            }
            
            lastBean.setCompId(compId);
            lastBean.setRevenue("0");
            lastBean.setIpInvent("0");
            lastBean.setIpNewType("0");
            lastBean.setIpLook("0");
            lastBean.setIcDrawing("0");
            lastBean.setSoftwareProd("0");
            lastBean.setSoftwareBook("0");
            lastBean.setDoctor("0");
            lastBean.setMaster("0");
            lastBean.setBachelor("0");
            lastBean.setCollege("0");
            lastBean.setTotalEmployee("0");
            
            monthReportDao.addMonthReport(lastBean);
        }
    }
    
    @Override
    public List<MonthReportBean> queryMonthListComp(String compId, String year)
    {
        // TODO Auto-generated method stub
        return monthReportDao.queryMonthListComp(compId, year);
    }
    
    @Override
    public List<MonthReportBean> queryAllMonthListComp(String yearnum, String monthnum)
    {
        List<MonthReportBean> list = monthReportDao.queryAllMonthListComp(yearnum, monthnum);
        for (int i = 0; i < list.size(); i++)
        {
            MonthReportBean report = list.get(i);
            String compId = report.getCompId();
            if (compId != null && !"总计".equals(compId) && !"Total".equalsIgnoreCase(compId))
            {
                CompanyBean company = new CompanyBean();
                company.setId(Integer.parseInt(compId));
                CompanyBean companyBean = companyDao.getCompany(company);
                if(companyBean != null)
                {
                    report.setCompName(companyBean.getCompanyname());
                }
            }
        }
        return list;
    }
    
    @Override
    public void addMonthReport(MonthReportBean bean)
    {
        monthReportDao.addMonthReport(bean);
    }
    
    @Override
    public void addLastReport(MonthReportBean bean)
    {
        monthReportDao.addLastReport(bean);
    }
    
    @Override
    public List<MonthReportBean> queryMonthTotal(String benginYear, String endYear, String beginMonth, String endMonth)
    {
        List<MonthReportBean> list = monthReportDao.queryMonthTotal(benginYear, endYear, beginMonth, endMonth);
        return list;
    }
    
    @Override
    public List<MonthReportBean> queryQuartTotal(String benginYear, String endYear, String beginQuart, String endQuart)
    {
        List<MonthReportBean> list = monthReportDao.queryQuartTotal(benginYear, endYear, beginQuart, endQuart);
        return list;
    }
    
    @Override
    public List<MonthReportBean> queryYearTotal(String benginYear, String endYear)
    {
        return monthReportDao.queryYearTotal(benginYear, endYear);
    }
    
    @Override
    public MonthReportBean queryReportExist(String compId, String yearnum, String monthnum)
    {
        return monthReportDao.queryReportExist(compId, yearnum, monthnum);
    }
    
    @Override
    public void modifyMonthReport(MonthReportBean bean)
    {
        monthReportDao.modifyMonthReport(bean);
    }
    
    @Override
    public MonthReportBean queryLastReportExist(String compId)
    {
        return monthReportDao.queryLastReportExist(compId);
    }
    
    @Override
    public List<MonthReportBean> queryQuartTotalComp(String year, String quart)
    {
        List<MonthReportBean> list = monthReportDao.queryQuartTotalComp(year, quart);
        for (int i = 0; i < list.size(); i++)
        {
            MonthReportBean report = list.get(i);
            String compId = report.getCompId();
            if (compId != null && !"总计".equals(compId) && !"Total".equalsIgnoreCase(compId))
            {
                CompanyBean company = new CompanyBean();
                company.setId(Integer.parseInt(compId));
                CompanyBean companyBean = companyDao.getCompany(company);
                if(companyBean != null)
                {
                    report.setCompName(companyBean.getCompanyname());
                }
            }
        }
        return list;
    }
    
    @Override
    public List<MonthReportBean> queryYearTotalComp(String year)
    {
        List<MonthReportBean> list = monthReportDao.queryYearTotalComp(year);
        for (int i = 0; i < list.size(); i++)
        {
            MonthReportBean report = list.get(i);
            String compId = report.getCompId();
            if (compId != null && !"总计".equals(compId) && !"Total".equalsIgnoreCase(compId))
            {
                CompanyBean company = new CompanyBean();
                company.setId(Integer.parseInt(compId));
                CompanyBean companyBean = companyDao.getCompany(company);
                if(companyBean != null)
                {
                    report.setCompName(companyBean.getCompanyname());
                }
            }
        }
        return list;
    }
}
